import positionsUpdataTpl from '../../views/positions/positions-updata.art'
import positionsUpdataFormTpl from '../../views/positions/positions-updata-form.art'
import { positionsUpdata as positionsUpdataModel, positionsListone } from '../../models/positions'

// 编辑职位
export const updataPosition = () => {
  $('#positions-list-box').after(positionsUpdataTpl())

  const _save = async () => {
    // 提交表单
    // const data = $('#positions-form').serialize()
    // let res = await positionsAddModel(data)
    let res = await positionsUpdataModel()
    try {
      if(res.ret) {
        alert(res.data.message)
        // _loadData() // 添加数据后渲染
        $('body').trigger('updataPosition')
        $('#positions-close-updata').click()
      } else {
        alert(res.data.message)
      }
    } catch (err) {
      console.log(err)
    }
  }
    
  // 点击保存，提交表单
  $('#positions-save-updata').off('click').on('click',  _save)
}

export const fillPositionsUpdataTpl = async (id) => {
  let { data } = await positionsListone(id)
  $('#positions-form-updata').html(positionsUpdataFormTpl({data: data.result}))
}